function paresPostData(ctx) {
  return new Promise((resolve, reject) => {
    try {
      const body = [];
      ctx.req.addListener('data', (data) => {
        body.push(data);
      });
      ctx.req.on('end', () => {
        const chunks = Buffer.concat(body);
        console.log('data===', chunks.toString());
        resolve(chunks.toString());
      });
    } catch (err) {
      reject(err);
    }
  });
}

const postDataToJson = () => {
  return async (ctx, next) => {
    try {
      if (ctx.req?.headers?.['content-type']?.indexOf?.('application/json') !== -1) {
        const data = await paresPostData(ctx);
        if (!data) return await next();
        const strictJSONReg = /^[\x20\x09\x0a\x0d]*(\[|\{)/;
        if (!strictJSONReg.test(data)) {
          throw new Error('invalid JSON, only supports object and array');
        }
        ctx.data = JSON.parse(data);
      }
    } catch (e) {
      console.error('数据解析错误：', e);
    }
    await next();
  };
};
export default postDataToJson;
